* ---------------------------------------------------------------------------------------------------
* enumerate_insurers: Constructs list of insurers in the SPMI/NAIC data
* ---------------------------------------------------------------------------------------------------

* --------------------------------------
* Import lists of companies
* --------------------------------------

* List of companies: Life
import excel using "$raw/sp_insurance/Firms_List.xlsm", clear sheet("Life")
cap drop A C
foreach var of varlist * {
    replace `var' = "assets_" + lower(`var') if _n == 1
}
replace B = "firm" if _n == 1
foreach var of varlist * {
     local try = strtoname(`var'[1]) 
     capture rename `var' `try' 
}
cap drop if _n == 1
foreach var of varlist assets_* {
    destring `var', force replace
}
save $temp/insurance/lists/life_assets, replace


* List of companies: Health
import excel using "$raw/sp_insurance/Firms_List.xlsm", clear sheet("Health")
cap drop A C
foreach var of varlist * {
    replace `var' = "assets_" + lower(`var') if _n == 1
}
replace B = "firm" if _n == 1
foreach var of varlist * {
     local try = strtoname(`var'[1]) 
     capture rename `var' `try' 
}
cap drop if _n == 1
foreach var of varlist assets_* {
    destring `var', force replace
}
save $temp/insurance/lists/health_assets, replace


* List of companies: P&C
import excel using "$raw/sp_insurance/Firms_List.xlsm", clear sheet("PC")
cap drop A C
foreach var of varlist * {
    replace `var' = "assets_" + lower(`var') if _n == 1
}
replace B = "firm" if _n == 1
foreach var of varlist * {
     local try = strtoname(`var'[1]) 
     capture rename `var' `try' 
}
cap drop if _n == 1
foreach var of varlist assets_* {
    destring `var', force replace
}
save $temp/insurance/lists/pc_assets, replace

* --------------------------------------
* Process individual periods
* --------------------------------------

* Looping over time periods
forval i=1/64 {

	* Get date
	local base_date = tq(2005q1)
	local date_q = `base_date' + `i' - 1
	local year = year(dofq(`date_q'))

	* Get life insurers list
	use $temp/insurance/lists/life_assets.dta, clear
	keep firm assets_`year'y
	drop if firm == "Life Industry"
	gsort -assets_`year'y
	drop if assets_`year'y < 0
	drop if missing(assets_`year'y)
	drop if assets_`year'y == 0
	save $temp/insurance/lists/life_insurers_list_`year', replace

	* Get health insurers list
	use $temp/insurance/lists/health_assets.dta, clear
	keep firm assets_`year'y
	drop if firm == "Health Industry"
	gsort -assets_`year'y
	drop if assets_`year'y < 0
	drop if missing(assets_`year'y)
	drop if assets_`year'y == 0
	save $temp/insurance/lists/health_insurers_list_`year', replace

	* Get p&c insurers list
	use $temp/insurance/lists/pc_assets.dta, clear
	keep firm assets_`year'y
	drop if firm == "P&C Industry"
	gsort -assets_`year'y
	drop if assets_`year'y < 0
	drop if missing(assets_`year'y)
	drop if assets_`year'y == 0
	save $temp/insurance/lists/pc_insurers_list_`year', replace

}
